package ar.edu.uba.fi.jmit.aspecter.monitor.invocationCounter;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javassist.ClassPool;
import javassist.CtClass;
import ar.edu.uba.fi.jmit.aspecter.AspecterTestCase;
import ar.edu.uba.fi.jmit.aspecter.core.Constants;
import ar.edu.uba.fi.jmit.aspecter.core.ConstructorDefinition;
import ar.edu.uba.fi.jmit.aspecter.core.ResourceProperties;
import ar.edu.uba.fi.jmit.aspecter.core.injector.Injector;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.EventPriorityMock;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MockEvent;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.SenderFactoryMock;

public class InvocationCounterConstructorInjectorTestCase extends
		AspecterTestCase {

	private ConstructorDefinition emptyConstructorDefinition;
	
	private ConstructorDefinition intConstructorDefinition;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		SenderFactoryMock.clean();
		emptyConstructorDefinition = new ConstructorDefinition(
				ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget.class,
				new LinkedList<Class<?>>());
		
		List<Class<?>> params = new LinkedList<Class<?>>();
		params.add(int.class);
		intConstructorDefinition = new ConstructorDefinition(
				ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget.class,
				params);
	}

	public void testInjectOnEmptyConstructor() {
		try {
			Injector injector = new InvocationCounterConstructorInjector(emptyConstructorDefinition, "monitoreo_testInjectOnGetXMethod", "id");
			CtClass ctClazz = ClassPool.getDefault().getAndRename("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", getNextClassName());
			injector.inject(ctClazz);
			Class<?> clazz = ctClazz.toClass();
			for(int i = 0; i< 15; i++) {
				assertEquals(i, SenderFactoryMock.getSenderInstance().getSendedEvents().size());
				clazz.newInstance();
			}
			
			for(int i = 0; i< 15; i++) {
				assertEquals(15, SenderFactoryMock.getSenderInstance().getSendedEvents().size());
				clazz.getConstructor(int.class).newInstance(10);
			}
			
			for(int i = 0; i< 15; i++) {
				assertEquals(i + 15, SenderFactoryMock.getSenderInstance().getSendedEvents().size());
				clazz.getConstructor(int.class, int.class).newInstance(10, 0);
			}
		} catch (Exception e) {
			fail(e);
		}
	}
	
	public void testObjectId() {
		try {
			Injector injector = new InvocationCounterConstructorInjector(intConstructorDefinition, "monitoreo_testInjectOnGetXMethod", "id");
			CtClass ctClazz = ClassPool.getDefault().getAndRename("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", getNextClassName());
			injector.inject(ctClazz);
			Class<?> clazz = ctClazz.toClass();
			clazz.newInstance();
			assertEquals(0, SenderFactoryMock.getSenderInstance().getSendedEvents().size());
			clazz.getConstructor(int.class).newInstance(10);
			assertEquals(1, SenderFactoryMock.getSenderInstance().getSendedEvents().size());
			List<MockEvent> list = SenderFactoryMock.getSenderInstance().getSendedEvents();
			assertEquals(10, list.get(0).getData().get(ResourceProperties.getProperty(Constants.EVENT_ID_OBJECT_KEY)));
			
		} catch (Exception e) {
			fail(e);
		} 
	}
	
	public void testObjectIdAsMethod() {
		try {
			Injector injector = new InvocationCounterConstructorInjector(intConstructorDefinition, "monitoreo_testInjectOnGetXMethod", "getId()");
			CtClass ctClazz = ClassPool.getDefault().getAndRename("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", getNextClassName());
			injector.inject(ctClazz);
			Class<?> clazz = ctClazz.toClass();
			clazz.getConstructor(int.class).newInstance(10);
			List<MockEvent> list = SenderFactoryMock.getSenderInstance().getSendedEvents();
			assertEquals(1, list.size());
			assertEquals(10, list.get(0).getData().get(ResourceProperties.getProperty(Constants.EVENT_ID_OBJECT_KEY)));
		} catch (Exception e) {
			fail(e);
		} 
	}
	
	public void testSendedData() {
		try {
			String className = getNextClassName();
			Injector injector = new InvocationCounterConstructorInjector(intConstructorDefinition, "monitoreo_testSendedData", "id");
			CtClass ctClazz = ClassPool.getDefault().getAndRename("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", className);
			injector.inject(ctClazz);
			Class<?> clazz = ctClazz.toClass();
			clazz.getConstructor(int.class).newInstance(10);
			List<MockEvent> list = SenderFactoryMock.getSenderInstance().getSendedEvents();
			assertEquals(10, list.get(0).getData().get(ResourceProperties.getProperty(Constants.EVENT_ID_OBJECT_KEY)));
			assertEquals("monitoreo_testSendedData", list.get(0).getData().get(ResourceProperties.getProperty(Constants.EVENT_MONITOR_NAME_KEY)));
			assertSimilarDates(new Date(), (Date) list.get(0).getData().get(ResourceProperties.getProperty(Constants.EVENT_MONITOR_DATE)));
			assertEquals("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget(int)", list.get(0).getSource());
			assertEquals(EventPriorityMock.NORMAL, list.get(0).getPriority());
		} catch (Exception e) {
			fail(e);
		} 
	}
}
